package com.zhl.media.commonres.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zhl.media.commonres.R;


public class UpdataDialog extends Dialog implements View.OnClickListener {

    private Context context;


    private OnCenterItemClickListener listener;
    private OnDownloadOkClickListener downloadOkClickListener;
    private TextView title;
    private TextView msg;
    private TextView button;
    private String titleText;
    private String msgText;
    private LinearLayout btn;
    private TextView progress;
    public UpdataDialog(Context context, String t, String m) {
        super(context, R.style.checkUpdateDialog);
        this.context = context;
        this.titleText = t;
        this.msgText = m;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = getWindow();
        window.setGravity(Gravity.CENTER);
        window.setWindowAnimations(R.style.bottom_menu_animation);
        setContentView(R.layout.public_layout_dialog_checkupdate);
        WindowManager windowManager = ((Activity) context).getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = display.getWidth()*5/6;
        getWindow().setAttributes(lp);
        setCanceledOnTouchOutside(false);
        btn = (LinearLayout)findViewById(R.id.dialog_sure);
        btn.setOnClickListener(this);
        button = (TextView) findViewById(R.id.btn_text);
        title = (TextView) findViewById(R.id.tv_up_title);
        msg = (TextView) findViewById(R.id.tv_up_msg);
        progress = (TextView) findViewById(R.id.progress_info);
        title.setText(titleText);
        msg.setText(msgText);
        setOnKeyListener(new OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialogInterface, int i, KeyEvent keyEvent) {
                return true;
            }
        });
    }
    public interface OnCenterItemClickListener {
        void OnCenterItemClick(UpdataDialog dialog, View view);
    }
    public interface OnDownloadOkClickListener {
        void OnClick();
    }

    public void setOnCenterItemClickListener(OnCenterItemClickListener listener) {
        this.listener = listener;
    }

    public void setOnDownloadOkClickListener(OnDownloadOkClickListener listener) {
        this.downloadOkClickListener = listener;
    }

    public void l(){
        btn.setVisibility(View.GONE);
        progress.setVisibility(View.VISIBLE);
    }

    public void setDownloadOkClick(){
        progress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                downloadOkClickListener.OnClick();
            }
        });
    }

    public void setButtonText(String text){
        progress.setText(text);
    }

    @Override
    public void onClick(View view) {
        listener.OnCenterItemClick(this, view);
    }
}